package com.feng.show.controller;


import com.feng.show.comm.ResponseCode;
import com.feng.show.comm.ServerResponse;
import com.feng.show.entity.ProductType;
import com.feng.show.service.IAddProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
public class AddProductTypeController {

    @Autowired
    private IAddProductTypeService addProductTypeService;


    @PostMapping("/addType")
    public ServerResponse<String> label(

            @RequestParam(value = "no") String no,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "des") String des ,
            @RequestParam(value = "userId") String userId)

    {


        if(no==null||name==null||des==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");

        }else{
            return addProductTypeService.addProductType(name,no,des);
        }

    }

    @PostMapping("/modifyType")
    public ServerResponse<String> modifyType(
            @RequestParam(value = "id") String id,
            @RequestParam(value = "no") String no,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "des") String des ,
            @RequestParam(value = "userId") String userId)

    {


        if(no==null||name==null||des==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");

        }else{
            Integer iid=Integer.valueOf(id);
            ProductType productType=new ProductType(iid,no,name,des);
            productType.setUpdateTime(new Date());
            return addProductTypeService.modifyProductType(productType);
        }

    }

    @PostMapping("/getProductTypeList")
    public ServerResponse<List<ProductType>> getProductTypeList(

            @RequestParam(value = "userId") String userId)

    {

        userId="10001";
        if(userId==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");

        }else{
            return addProductTypeService.getProductTypeList(userId);
        }

    }

  @PostMapping("/delProductype")
  public ServerResponse<String> delProductType(

    @RequestParam(value = "userId") String userId,@RequestParam(value = "pId") String pId)

  {



    userId="10001";
    if(userId==null){
      return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");

    }

    if(pId==null|| pId.isEmpty()|| pId.trim().isEmpty()){
      return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"参数错误");
    }
    return addProductTypeService.delProductType(pId);


  }







}
